﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using CommunityServer.WebServices.MediaGalleries;

namespace nb.development.csmediamanager
{
    public class AttachmentPicker : VistaControls.Button
    {

        public event TemporaryMediaUploaded UploadCompleted;

        public AttachmentPicker()
        {
            this.MouseClick += new MouseEventHandler(AttachmentPicker_MouseClick);
            this.uploaded = false;
        }

        private bool uploaded;

        public bool Uploaded
        {
            get { return uploaded; }
        }

        private MediaGallery gallery;

        public MediaGallery Gallery
        {
            get { return gallery; }
            set { gallery = value; }
        }

        private Guid siteId;

        public Guid SiteId
        {
            get { return siteId; }
            set { siteId = value; }
        }

        private Guid temporaryGuid;

        public Guid TemporaryGuid
        {
            get { return temporaryGuid; }
        }

        private string filename;

        public string Filename
        {
            get { return filename; }
            set { filename = value; }
        }

        void AttachmentPicker_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                FileAttachment fa = new FileAttachment(gallery, this.siteId);
                if (fa.ShowDialog() == DialogResult.OK)
                {
                    try
                    {
                        temporaryGuid = fa.TemporaryPostAttachment;
                        this.filename = fa.Filename;
                        this.uploaded = true;
                        UploadCompleted(sender);
                    }
                    catch (Exception ex) { MessageBox.Show("Error: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); }
                }
            }
        }

        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // AttachmentPicker
            // 
            this.Font = new System.Drawing.Font("Segoe UI", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.ResumeLayout(false);

        }

    }
    public delegate void TemporaryMediaUploaded(object sender);
}
